/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.murillo.chufa;

import javax.xml.stream.XMLStreamException;
import org.murillo.utils.debug.Memory;
import org.murillo.utils.debug.Timer;

/**
 *
 * @author Administrador
 */
public class Main {

    public static void main(String[] args) throws XMLStreamException, IllegalArgumentException, IllegalAccessException, InterruptedException {
        Timer t = new Timer();
        final Cache c = new Cache(1000, -1, false);
//        final int[][] macro = new int[1000000][];
//        for (int i = 0; i < macro.length; i++) {
//            int[] m = new int[i / 3000];
//            macro[i] = m;
//        }
//        com.google.common.cache.Cache<Integer, int[]> build = CacheBuilder.newBuilder().softValues().initialCapacity(10000).concurrencyLevel(2).build();
        t.start();
        int h = 0;

//        for(int i = 0 ; i<macro.length ; i++){
//            int[]m = macro[i];
//            build.put(i, m);
//            macro[i] = null;
//            Object get = build.getIfPresent(macro.length-i-1);
//            if(get!=null) h++;
//        }
//        System.out.println("Guava: "+t.elapsedInMillis());
//        System.out.println("Hits: "+h);
//        
//        for(int i = 0 ; i<macro.length ; i++){
//            int[]m = new int[i/50000];
//            macro[i] = m;
//        }
//        
//        
//        t.start();
//        h = 0;
        final int length = 500000;
        Thread t1 = new Thread(new Runnable() {

            @Override
            public void run() {
                for (int i = 0; i < length; i++) {
                    int[] m = new int[i / 3000];
                    c.put(i, m, m.length);
                    c.addFutureUse(i);
                    Object get = c.get(length - i - 1);
//                    if(get!=null) System.out.println("EXITO");
//                    else System.out.println("FALLO");
                }
            }
        });

        Thread t2 = new Thread(new Runnable() {

            @Override
            public void run() {
                for (int i = 0; i < length; i++) {
                    int[] m = new int[i / 3000];
                    c.put(length - i - 1, m, m.length);
                    c.addFutureUse(i);
                    Object get = c.get(i);
//                    if(get!=null) System.out.println("EXITO");
//                    else System.out.println("FALLO");
                }
            }
        });
        
        Thread t3 = new Thread(new Runnable() {

            @Override
            public void run() {
                for (int i = 0; i < length; i++) {
                    int[] m = new int[i / 3000];
                    c.put(i, m, m.length);
                    c.addFutureUse(i);
                    Object get = c.get(length - i - 1);
//                    if(get!=null) System.out.println("EXITO");
//                    else System.out.println("FALLO");
                }
            }
        });

        Thread t4 = new Thread(new Runnable() {

            @Override
            public void run() {
                for (int i = 0; i < length; i++) {
                    int[] m = new int[i / 3000];
                    c.put(length - i - 1, m, m.length);
                    c.addFutureUse(i);
                    Object get = c.get(i);
//                    if(get!=null) System.out.println("EXITO");
//                    else System.out.println("FALLO");
                }
            }
        });

        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t1.join();
        t2.join();
        t3.join();
        t4.join();
        
        System.out.println("Chufa: " + t.elapsedInMillis());
        System.out.println("Hits: " + h);
        System.out.println("Hard size: " + c.getHardSize());
        System.out.println("Soft size: " + c.getSoftSize());
//        System.out.println("Guava size: "+build.size());

//        System.out.println("sizeOf Chufa: " + Arrays.toString(Memory.sizeOf(new Integer(123))));
        System.out.println(Runtime.getRuntime().totalMemory());
        System.out.println("sizeOf Chufa: " + Memory.sizeOf(c));
//        System.out.println("sizeOf Guava: " + Memory.sizeOf(build));
//        System.out.println(new ReflectToString().toString(c));
    }
    
    
}
